﻿using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using NFine.Data;
using NFine.Data.Dto.SystemManage;
using NFine.Data.Entity.SystemManage;
using NFine.Util;
using SharpRepository.Repository;
using SharpRepository.Repository.Configuration;
using System;
using System.Data.SqlClient;

namespace NFine.Repository.SystemManage
{
    public class TenantRepository : ConfigurationBasedRepository<Tenant, long>
    {
        public TenantRepository(ISharpRepositoryConfiguration configuration, string repositoryName = null) : base(configuration, repositoryName)
        {

        }

        public void CreateTenant(TenantInputDto tenantInputDto)
        {
            long operatorId = OperatorProvider.Provider.GetOperator().Id;
            long tenantId = IdWorkerHelper.GenId64();
            long organizeId = IdWorkerHelper.GenId64();
            long sysItemsId = IdWorkerHelper.GenId64();
            long roleTypeId = IdWorkerHelper.GenId64();
            long organizeTypeId = IdWorkerHelper.GenId64();

            long roleId = IdWorkerHelper.GenId64();
            long userId = IdWorkerHelper.GenId64();
            long userRoleId = IdWorkerHelper.GenId64();

            long systemManageId = IdWorkerHelper.GenId64();
            long roleManageId = IdWorkerHelper.GenId64();
            long positionManageId = IdWorkerHelper.GenId64();
            long dictItemManageId = IdWorkerHelper.GenId64();
            long organizeManageId = IdWorkerHelper.GenId64();
            long resourceManageId = IdWorkerHelper.GenId64();
            long userManageId = IdWorkerHelper.GenId64();
            long regionManageId = IdWorkerHelper.GenId64();

            using (var context = new NFineDbContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        #region 租户
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_tenant`(`Id`, `TenantName`, `FullName`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@TenantId, @TenantName, @FullName, b'1', b'0', @Description, @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[]{
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@TenantName", tenantInputDto.TenantName),
                            new MySqlParameter("@FullName", tenantInputDto.FullName),
                            new MySqlParameter("@Description", tenantInputDto.Description),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 组织机构
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_organize`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `ShortName`, `CategoryId`, `ObjectType`, `ManagerId`, `TelePhone`, `MobilePhone`, `WeChat`, `Fax`, `Email`, `RegionId`, `Address`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@OrganizeId, @TenantId, 0, 1, 'Company', '初始化组织机构，请自行修改', '初始化组织机构，请自行修改', NULL, 'Company', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, b'0', b'1', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@OrganizeId", organizeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 岗位
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_position`(`Id`, `TenantId`, `OrganizeId`, `EnCode`, `FullName`, `AllowEdit`, `AllowDelete`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @OrganizeId, 'ProjectManager', '项目经理', b'0', b'0', 2, b'1', b'0', '&nbsp;', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@OrganizeId", organizeId),                            
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_position`(`Id`, `TenantId`, `OrganizeId`, `EnCode`, `FullName`, `AllowEdit`, `AllowDelete`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @OrganizeId, 'ProductManager', '产品经理', b'0', b'0', 2, b'1', b'0', '&nbsp;', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@OrganizeId", organizeId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_position`(`Id`, `TenantId`, `OrganizeId`, `EnCode`, `FullName`, `AllowEdit`, `AllowDelete`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @OrganizeId, 'SoftwareEngineer ', '软件工程师', b'0', b'0', 2, b'1', b'0', '&nbsp;', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@OrganizeId", organizeId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 数据字典
                        #region 数据字典类型
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict`(`Id`, `TenantId`, `ParentId`, `EnCode`, `FullName`, `IsTree`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES(@DictId, @TenantId, 0, 'Sys_Items', '通用字典', b'0', NULL, 1, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@DictId", sysItemsId),
                                new MySqlParameter("@TenantId", tenantId),
                                new MySqlParameter("@CreatedTime", System.DateTime.Now),
                                new MySqlParameter("@CreatedUserId", operatorId)
                            });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict`(`Id`, `TenantId`, `ParentId`, `EnCode`, `FullName`, `IsTree`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES(@RoleTypeId, @TenantId, @ParentId, 'RoleType', '角色类型', b'0', NULL, 1, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@RoleTypeId", roleTypeId),
                            new MySqlParameter("@ParentId", sysItemsId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict`(`Id`, `TenantId`, `ParentId`, `EnCode`, `FullName`, `IsTree`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES(@OrganizeTypeId, @TenantId, @ParentId, 'OrganizeType', '组织机构类型', b'0', NULL, 1, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@OrganizeTypeId", organizeTypeId),
                            new MySqlParameter("@ParentId", sysItemsId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion
                        #region 数据字典项目 角色类型（系统角色、业务角色、其他角色）
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @RoleTypeId, @TenantId, 0, 'SystemRole', '系统角色', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@RoleTypeId", roleTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @RoleTypeId, @TenantId, 0, 'BusinessRole', '业务角色', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@RoleTypeId", roleTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @RoleTypeId, @TenantId, 0, 'OtherRole', '其他角色', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@RoleTypeId", roleTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion
                        #region 数据字典项目 组织机构类型（集团、公司、部门、小组）
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @OrganizeTypeId, @TenantId, 0, 'Group', '集团', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@OrganizeTypeId", organizeTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @OrganizeTypeId, @TenantId, 0, 'Company', '公司', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@OrganizeTypeId", organizeTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @OrganizeTypeId, @TenantId, 0, 'Department', '部门', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@OrganizeTypeId", organizeTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_dict_item`(`Id`, `DictId`, `TenantId`, `ParentId`, `ItemCode`, `ItemName`, `IsDefault`, `Layers`, `SortCode`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @OrganizeTypeId, @TenantId, 0, 'Team', '小组', NULL, 0, NULL, b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@OrganizeTypeId", organizeTypeId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion
                        #endregion

                        #region 角色
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_role`(`Id`, `TenantId`, `OrganizeId`, `FullName`, `EnCode`, `SortCode`, `AllowEdit`, `AllowDelete`, `ObjectType`, `EnabledMark`, `DeletedMark`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@RoleId, @TenantId, @OrganizeId, 'Admin', 'Admin', 1, b'0', b'0', 'SystemRole', b'1', b'0', '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@RoleId", roleId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@OrganizeId", organizeId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 用户
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_user`(`Id`, `TenantId`, `UserName`, `Password`, `SecretKey`, `NickName`, `RealName`, `Email`, `TelePhone`, `MobilePhone`, `WeChat`, `Birthday`, `ObjectType`, `Gender`, `LastLoginTime`, `EnabledMark`, `DeletedMark`, `IsAdministrator`, `CompanyId`, `DepartmentId`, `PositionId`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@UserId, @TenantId, 'Admin', '38f2190d1e0f6863c37a04ed25453b0f', '31c2cc5d9385c6e6', 'Admin', 'Admin', 'Admin', 'Admin', 'Admin', 'Admin', NULL, NULL, NULL, NULL, b'1', b'0', NULL, NULL, NULL, NULL, NULL, @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@UserId", userId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 用户角色
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_user_role`(`Id`, `TenantId`, `UserId`, `RoleId`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `DeletedTime`, `DeleterUserId`, `ModifiedTime`, `ModifierUserId`) VALUES (@UserRoleId, @TenantId, @UserId, @RoleId, b'0', NULL, NULL, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@UserRoleId", userRoleId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@UserId", userId),
                            new MySqlParameter("@RoleId", roleId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 资源菜单
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, 0, NULL, NULL, '系统管理', 'fa fa-th-large', '', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", systemManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '角色管理', 'fa fa-th-large', '/SystemManage/Role/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", roleManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '岗位管理', 'fa fa-th-large', '/SystemManage/Position/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", positionManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '数据字典', 'fa fa-th-large', '/SystemManage/DictItem/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", dictItemManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '组织机构', 'fa fa-th-large', '/SystemManage/Organize/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", organizeManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '资源菜单', 'fa fa-th-large', '/SystemManage/Resource/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", resourceManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '用户管理', 'fa fa-th-large', '/SystemManage/User/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", userManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_resource`(`Id`, `TenantId`, `ParentId`, `Layers`, `EnCode`, `FullName`, `Icon`, `UrlAddress`, `Target`, `IsMenu`, `IsExpand`, `IsPublic`, `IsDisplay`, `AllowEdit`, `AllowDelete`, `SortCode`, `DeletedMark`, `EnabledMark`, `ObjectType`, `Location`, `JsEvent`, `Split`, `PermissionCode`, `Module`, `Description`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ParentId, NULL, NULL, '区域管理', 'fa fa-th-large', '/SystemManage/Region/Index', 'expand', b'1', b'1', b'1', b'1', b'1', b'0', 101, b'0', b'1', 'Menu', NULL, NULL, NULL, 'SystemManage', NULL, '', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", regionManageId),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ParentId", systemManageId),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion

                        #region 资源菜单权限
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", systemManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", roleManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", positionManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", dictItemManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", organizeManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", resourceManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", userManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        context.Database.ExecuteSqlCommand("INSERT INTO `sys_permission`(`Id`, `TenantId`, `ResourceId`, `ObjectId`, `ObjectType`, `SortCode`, `DeletedMark`, `CreatedTime`, `CreatorUserId`, `ModifiedTime`, `ModifierUserId`, `DeletedTime`, `DeleterUserId`) VALUES (@Id, @TenantId, @ResourceId, @ObjectId, @ObjectType, NULL, b'0', @CreatedTime, @CreatedUserId, NULL, NULL, NULL, NULL);", new[] {
                            new MySqlParameter("@Id", IdWorkerHelper.GenId64()),
                            new MySqlParameter("@TenantId", tenantId),
                            new MySqlParameter("@ResourceId", regionManageId),
                            new MySqlParameter("@ObjectId", roleId),
                            new MySqlParameter("@ObjectType", "RolePermission"),
                            new MySqlParameter("@CreatedTime", System.DateTime.Now),
                            new MySqlParameter("@CreatedUserId", operatorId)
                        });
                        #endregion
                        context.Database.CommitTransaction();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
    }
}